package assignmentstrategy

import (
	"codejy/analysis/codeanalysis"
	"codejy/structdef"
	"codejy/utils"
	"strings"
)

type StrStrategy struct {
}

func init() {
	RegisterStrategy(&StrStrategy{})
}

func (n StrStrategy) getId() int {
	return codeanalysis.STR
}

func (n StrStrategy) Execute(threadStore *structdef.ThreadStore, code string) string {
	//处理占位符
	brackets, p := utils.GetStrBigBrackets(code)
	if p {
		for i := range brackets {
			value := AssignmentStrategyMode(threadStore, brackets[i])
			//如果是字符串,那么就去掉""
			if strings.HasPrefix(value, "\"") && strings.HasSuffix(value, "\"") {
				value = value[1 : len(value)-1]
			}
			//替换
			code = strings.ReplaceAll(code, "${"+brackets[i]+"}", value)
		}
	}
	return code
}
